package pokestar;

import comunicacao.*;
import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import objectos.Objecto;
import objectos.TipoObjecto;

/**
 * @author igo
 * Classe PokeStar
 * Classe Principal do Cliente
 */
public class PokeStar {

    private String PS_name = "PokeStars";
    private String PS_version = "1.0";
    private String PS_author = "Rodrigo Correia";
    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    private Socket socket;
    private final int PORT = 25000;
    private JTextField servidor;
    private JTextField username;
    private String JogoS = "";
    private Mapa m;
    private JPanel info;
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        PokeStar PS = new PokeStar();
        PS.start();
    }

    /**
     * método que inicia o Cliente com o Splash Screen
     */
    private void start() {
        show_splash();
    }

    /**
     * Método que centra uma janela no ecrã, independentemente do tamanho do ecrã.
     * @param j - Recebe uma JFrame que irá centrar no ecrã.
     * @param s - Recebe a dimensão que terá a janela.
     */
    private void center(JFrame j, Dimension s) {
        j.setSize(s);

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int location_Y = (int) (dim.getHeight() / 2 - s.getHeight() / 2);
        int location_X = (int) (dim.getWidth() / 2 - s.getWidth() / 2);

        j.setLocation(location_X, location_Y);
    }

    /**
     * Mostra a janela onde é possível fazer o Registo de um novo Jogador
     */
    private void show_registar() {
        final JFrame registo = new JFrame();
        registo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container c_registo = registo.getContentPane();
        c_registo.setLayout(new GridLayout(1, 1));
        registo.setUndecorated(true);
        registo.setTitle(PS_name + " * Novo Registo");
        
        ClassLoader cl = this.getClass().getClassLoader();
        String img = "imagens/registo.png";
        URL icon = cl.getResource(img);
        ImageIcon Imagem;
        
        if(icon!=null)
            Imagem = new ImageIcon(icon);
        else
            Imagem = new ImageIcon(img);
        
        BGImage fundo = new BGImage(Imagem.getImage());
        center(registo, fundo.getSize());
        fundo.setLayout(new BorderLayout());

        JButton b_sair = new JButton("X");
        JButton b_registar = new JButton("Registar");
        JLabel l_user = new JLabel("Username");
        JLabel l_pass = new JLabel("Password");
        JLabel l_pcon = new JLabel("Confirmar Password");
        JLabel l_titulo = new JLabel("Novo Registo");
        final JTextField user = new JTextField(20);
        final JTextField pass = new JTextField(20);
        final JTextField pconf = new JTextField(20);

        b_registar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                if (user.getText().isEmpty() || pass.getText().isEmpty() || pconf.getText().isEmpty()) {
                    JOptionPane.showMessageDialog(registo, "Nenhum Campo Pode Ser Vazio", "Erro no Registo", JOptionPane.ERROR_MESSAGE);
                } else if (!pass.getText().equals(pconf.getText())) {
                    JOptionPane.showMessageDialog(registo, "As Passwords Não São Iguais!", "Erro no Registo", JOptionPane.ERROR_MESSAGE);
                } else {
                    try {
                        Registo R = new Registo(user.getText(), pass.getText());
                        oos.reset();
                        oos.writeObject(R);
                        oos.flush();
                        Confirmacao CR = (Confirmacao) ois.readObject();

                        if (CR.getResposta()) {
                            JOptionPane.showMessageDialog(registo, "O Registo foi feito com sucesso", "Registo com Sucesso", JOptionPane.INFORMATION_MESSAGE);
                        } else {
                            JOptionPane.showMessageDialog(registo, CR.getText(), "Erro no Registo", JOptionPane.ERROR_MESSAGE);
                        }
                        
                        show_menu();
                        registo.dispose();
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });

        b_sair.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                show_menu();
                registo.dispose();
            }
        });

        JPanel painel_norte = new JPanel();
        painel_norte.setOpaque(false);
        JPanel titulo = new JPanel();
        titulo.setOpaque(false);
        painel_norte.setLayout(new BorderLayout());
        titulo.setLayout(new FlowLayout());
        titulo.add(l_titulo);
        painel_norte.add(titulo, BorderLayout.CENTER);
        painel_norte.add(b_sair, BorderLayout.EAST);

        JPanel painel_sul = new JPanel();
        painel_sul.setOpaque(false);
        painel_sul.setLayout(new FlowLayout());
        painel_sul.add(b_registar);

        JPanel painel_centro = new JPanel();
        painel_centro.setOpaque(false);
        painel_centro.setLayout(new GridLayout(3, 1));
        JPanel p_user = new JPanel();
        p_user.setOpaque(false);
        p_user.setLayout(new BorderLayout());
        JPanel p_pass = new JPanel();
        p_pass.setOpaque(false);
        p_pass.setLayout(new BorderLayout());
        JPanel p_conf = new JPanel();
        p_conf.setOpaque(false);
        p_conf.setLayout(new BorderLayout());

        JPanel lu = new JPanel();
        lu.setOpaque(false);
        lu.setLayout(new FlowLayout());
        lu.add(l_user);
        JPanel lp = new JPanel();
        lp.setOpaque(false);
        lp.setLayout(new FlowLayout());
        lp.add(l_pass);
        JPanel lc = new JPanel();
        lc.setOpaque(false);
        lc.setLayout(new FlowLayout());
        lc.add(l_pcon);

        p_user.add(lu, BorderLayout.WEST);
        p_user.add(user, BorderLayout.EAST);
        p_pass.add(lp, BorderLayout.WEST);
        p_pass.add(pass, BorderLayout.EAST);
        p_conf.add(lc, BorderLayout.WEST);
        p_conf.add(pconf, BorderLayout.EAST);

        painel_centro.add(p_user);
        painel_centro.add(p_pass);
        painel_centro.add(p_conf);

        fundo.add(painel_norte, BorderLayout.NORTH);
        fundo.add(painel_centro, BorderLayout.CENTER);
        fundo.add(painel_sul, BorderLayout.SOUTH);
        c_registo.add(fundo);

        registo.addWindowListener(new WindowListener() {

            @Override
            public void windowOpened(WindowEvent we) {
                user.requestFocus();
            }

            @Override
            public void windowClosing(WindowEvent we) {
            }

            @Override
            public void windowClosed(WindowEvent we) {
            }

            @Override
            public void windowIconified(WindowEvent we) {
            }

            @Override
            public void windowDeiconified(WindowEvent we) {
            }

            @Override
            public void windowActivated(WindowEvent we) {
                user.requestFocus();
            }

            @Override
            public void windowDeactivated(WindowEvent we) {
            }
        });

        pconf.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent ke) {
            }

            @Override
            public void keyPressed(KeyEvent ke) {
                if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
                    if (user.getText().isEmpty() || pass.getText().isEmpty() || pconf.getText().isEmpty()) {
                        JOptionPane.showMessageDialog(registo, "Nenhum Campo Pode Ser Vazio", "Erro no Registo", JOptionPane.ERROR_MESSAGE);
                    } else if (!pass.getText().equals(pconf.getText())) {
                        JOptionPane.showMessageDialog(registo, "As Passwords Não São Iguais!", "Erro no Registo", JOptionPane.ERROR_MESSAGE);
                    } else {
                        try {
                            Registo R = new Registo(user.getText(), pass.getText());
                            oos.reset();
                            oos.writeObject(R);
                            oos.flush();
                            Confirmacao CR = (Confirmacao) ois.readObject();

                            if (CR.getResposta()) {
                                JOptionPane.showMessageDialog(registo, "O Registo foi feito com sucesso", "Registo com Sucesso", JOptionPane.INFORMATION_MESSAGE);
                            } else {
                                JOptionPane.showMessageDialog(registo, CR.getText(), "Erro no Registo", JOptionPane.ERROR_MESSAGE);
                            }
                            show_menu();
                            registo.dispose();
                        } catch (ClassNotFoundException ex) {
                            Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IOException ex) {
                            Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }

            @Override
            public void keyReleased(KeyEvent ke) {
            }
        });

        registo.setVisible(true);
    }

    /**
     * Mostra a janela onde se pode fazer o login do Jogador
     */
    private void show_login() {
        final JFrame login = new JFrame();
        login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        login.setUndecorated(true);
        login.setTitle(PS_name + " * Login");
        Container c_login = login.getContentPane();
        c_login.setLayout(new GridLayout(1, 1));

        ClassLoader cl = this.getClass().getClassLoader();
        String img = "imagens/login.jpg";
        URL icon = cl.getResource(img);
        ImageIcon Imagem;
        
        if(icon!=null)
            Imagem = new ImageIcon(icon);
        else
            Imagem = new ImageIcon(img);
        
        BGImage fundo = new BGImage(Imagem.getImage());
        
        center(login, fundo.getSize());
        JLabel l_user = new JLabel("Username");
        l_user.setForeground(Color.WHITE);
        JLabel l_pass = new JLabel("Password");
        l_pass.setForeground(Color.WHITE);
        JLabel l_titulo = new JLabel("Login");
        l_titulo.setForeground(Color.WHITE);
        username = new JTextField(20);
        final JTextField pass = new JTextField(20);
        JButton b_login = new JButton("Entrar");
        JButton b_sair = new JButton("X");

        fundo.setLayout(new BorderLayout());

        JPanel painel_norte = new JPanel();
        painel_norte.setOpaque(false);
        JPanel painel_sul = new JPanel();
        painel_sul.setOpaque(false);
        JPanel painel_centro = new JPanel();
        painel_centro.setOpaque(false);
        JPanel painel_username = new JPanel();
        painel_username.setOpaque(false);
        JPanel painel_password = new JPanel();
        painel_password.setOpaque(false);
        JPanel painel_login = new JPanel();
        painel_login.setOpaque(false);
        JPanel titulo = new JPanel();
        titulo.setOpaque(false);

        painel_centro.setLayout(new BorderLayout());
        painel_login.setLayout(new GridLayout(3, 1));
        painel_username.setLayout(new BorderLayout());
        painel_password.setLayout(new BorderLayout());

        JPanel lu = new JPanel();
        lu.setLayout(new FlowLayout());
        lu.add(l_user);
        lu.setOpaque(false);
        painel_username.add(lu, BorderLayout.WEST);
        painel_username.add(username, BorderLayout.EAST);

        JPanel lp = new JPanel();
        lp.setLayout(new FlowLayout());
        lp.add(l_pass);
        lp.setOpaque(false);
        painel_password.add(lp, BorderLayout.WEST);
        painel_password.add(pass, BorderLayout.EAST);

        painel_login.add(painel_username);
        painel_login.add(painel_password);

        painel_centro.add(painel_login, BorderLayout.CENTER);

        painel_sul.setLayout(new FlowLayout());
        painel_sul.add(b_login);

        titulo.setLayout(new FlowLayout());
        titulo.add(l_titulo);

        b_login.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {

                if (username.getText().isEmpty() || pass.getText().isEmpty()) {
                    JOptionPane.showMessageDialog(login, "Nenhum Campo Pode Estar Vazio", "Erro no Login", JOptionPane.ERROR_MESSAGE);
                } else {
                    try {
                        Login L = new Login(username.getText(), pass.getText());
                        oos.reset();
                        oos.writeObject(L);
                        oos.flush();
                        Confirmacao CL = (Confirmacao) ois.readObject();

                        if (CL.getResposta()) {
                            JOptionPane.showMessageDialog(login, "Bem-Vindo " + L.getUser() + "!", "Login", JOptionPane.INFORMATION_MESSAGE);
                            show_rooms();
                            login.dispose();
                        } else {
                            JOptionPane.showMessageDialog(login, CL.getText(), "Login", JOptionPane.ERROR_MESSAGE);
                            show_login();
                            login.dispose();
                        }
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });

        b_sair.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                show_menu();
                login.dispose();
            }
        });

        painel_norte.setLayout(new BorderLayout());
        painel_norte.add(titulo, BorderLayout.CENTER);
        painel_norte.add(b_sair, BorderLayout.EAST);

        fundo.add(painel_norte, BorderLayout.NORTH);
        fundo.add(painel_centro, BorderLayout.CENTER);
        fundo.add(painel_sul, BorderLayout.SOUTH);

        c_login.add(fundo);

        login.addWindowListener(new WindowListener() {

            @Override
            public void windowOpened(WindowEvent we) {
                username.requestFocus();
            }

            @Override
            public void windowClosing(WindowEvent we) {
            }

            @Override
            public void windowClosed(WindowEvent we) {
            }

            @Override
            public void windowIconified(WindowEvent we) {
            }

            @Override
            public void windowDeiconified(WindowEvent we) {
            }

            @Override
            public void windowActivated(WindowEvent we) {
                username.requestFocus();
            }

            @Override
            public void windowDeactivated(WindowEvent we) {
            }
        });

        pass.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent ke) {
            }

            @Override
            public void keyPressed(KeyEvent ke) {
                if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
                    if (username.getText().isEmpty() || pass.getText().isEmpty()) {
                        JOptionPane.showMessageDialog(login, "Nenhum Campo Pode Estar Vazio", "Erro no Login", JOptionPane.ERROR_MESSAGE);
                    } else {
                        try {
                            Login L = new Login(username.getText(), pass.getText());
                            oos.reset();
                            oos.writeObject(L);
                            oos.flush();
                            Confirmacao CL = (Confirmacao) ois.readObject();

                            if (CL.getResposta()) {
                                JOptionPane.showMessageDialog(login, "Bem-Vindo " + L.getUser() + "!", "Login", JOptionPane.INFORMATION_MESSAGE);
                                show_rooms();
                                login.dispose();
                            } else {
                                JOptionPane.showMessageDialog(login, CL.getText(), "Login", JOptionPane.ERROR_MESSAGE);
                                show_login();
                                login.dispose();
                            }
                        } catch (ClassNotFoundException ex) {
                            Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IOException ex) {
                            Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }

            @Override
            public void keyReleased(KeyEvent ke) {
            }
        });

        login.setVisible(true);
    }

    /**
     * Mostra o Splash Screen e faz a janela desaparecer após um curto espaço de tempo
     */
    private void show_splash() {
        JFrame splash;
        try {
            splash = new JFrame();
            splash.setUndecorated(true);
            splash.setTitle(PS_name);
            ClassLoader cl = this.getClass().getClassLoader();
            String img = "imagens/logo.jpg";
            URL icon = cl.getResource(img);
            ImageIcon Imagem;

            if(icon!=null)
                Imagem = new ImageIcon(icon);
            else
                Imagem = new ImageIcon(img);

            BGImage fundo = new BGImage(Imagem.getImage());
            center(splash, fundo.getSize());
            JLabel l_titulo = new JLabel(PS_name + " " + PS_version + " [Powered by " + PS_author + "]");

            Container c_splash = splash.getContentPane();
            c_splash.setLayout(new GridLayout(1, 1));

            fundo.setLayout(new BorderLayout());

            JPanel painel_norte = new JPanel();
            painel_norte.setLayout(new FlowLayout());
            painel_norte.setOpaque(false);

            painel_norte.add(l_titulo);

            fundo.add(painel_norte, BorderLayout.NORTH);

            c_splash.add(fundo);
            splash.setVisible(true);
            Thread.sleep(500);
            show_conection();
            splash.dispose();

        } catch (InterruptedException ex) {
            Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Mostra a janela onde poderá ser escolhido o servidor a ligar
     */
    private void show_conection() {
        final JFrame ligacao = new JFrame();
        ligacao.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ligacao.setUndecorated(true);
        ligacao.setTitle(PS_name + " * Ligação");
        Container c_servidor = ligacao.getContentPane();
        c_servidor.setLayout(new GridLayout(1, 1));

        ClassLoader cl = this.getClass().getClassLoader();
        String img = "imagens/servidor.png";
        URL icon = cl.getResource(img);
        ImageIcon Imagem;
        
        if(icon!=null)
            Imagem = new ImageIcon(icon);
        else
            Imagem = new ImageIcon(img);
        
        BGImage fundo = new BGImage(Imagem.getImage());
        center(ligacao, fundo.getSize());
        fundo.setLayout(new BorderLayout());

        JLabel l_titulo = new JLabel("Ligação ao Servidor");
        JLabel l_servidor = new JLabel("Servidor");
        servidor = new JTextField(20);
        JButton b_ligar = new JButton("Ligar");
        JButton b_sair = new JButton("Sair");

        JPanel ls = new JPanel();
        ls.setLayout(new FlowLayout());
        ls.setOpaque(false);
        ls.add(l_servidor);
        ls.add(servidor);
        fundo.add(ls, BorderLayout.CENTER);

        JPanel lt = new JPanel();
        lt.setLayout(new FlowLayout());
        lt.setOpaque(false);
        lt.add(l_titulo);
        fundo.add(lt, BorderLayout.NORTH);

        JPanel lb = new JPanel();
        lb.setLayout(new FlowLayout());
        lb.setOpaque(false);
        lb.add(b_ligar);
        lb.add(b_sair);
        fundo.add(lb, BorderLayout.SOUTH);

        b_sair.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                ligacao.dispose();
            }
        });

        b_ligar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                if (!servidor.getText().isEmpty()) {
                    try {
                        socket = new Socket(servidor.getText(), PORT);
                        oos = new ObjectOutputStream(socket.getOutputStream());
                        ois = new ObjectInputStream(socket.getInputStream());
                        show_menu();
                        ligacao.dispose();
                    } catch (UnknownHostException ex) {
                        //Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                        JOptionPane.showMessageDialog(ligacao, "Não foi possível efectuar a ligação ao servidor!", "Mensagem de Erro", JOptionPane.ERROR_MESSAGE);
                    } catch (IOException ex) {
                        //Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                        JOptionPane.showMessageDialog(ligacao, "Não foi possível efectuar a ligação ao servidor!", "Mensagem de Erro", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(ligacao, "O servidor não pode ficar em branco!", "Mensagem de Erro", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        c_servidor.add(fundo);

        ligacao.addWindowListener(new WindowListener() {

            @Override
            public void windowOpened(WindowEvent we) {
                servidor.requestFocus();
            }

            @Override
            public void windowClosing(WindowEvent we) {
            }

            @Override
            public void windowClosed(WindowEvent we) {
            }

            @Override
            public void windowIconified(WindowEvent we) {
            }

            @Override
            public void windowDeiconified(WindowEvent we) {
            }

            @Override
            public void windowActivated(WindowEvent we) {
                servidor.requestFocus();
            }

            @Override
            public void windowDeactivated(WindowEvent we) {
            }
        });

        servidor.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent ke) {
            }

            @Override
            public void keyPressed(KeyEvent ke) {
                if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
                    if (!servidor.getText().isEmpty()) {
                        try {
                            socket = new Socket(servidor.getText(), PORT);
                            oos = new ObjectOutputStream(socket.getOutputStream());
                            ois = new ObjectInputStream(socket.getInputStream());
                            show_menu();
                            ligacao.dispose();
                        } catch (UnknownHostException ex) {
                            //Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                            JOptionPane.showMessageDialog(ligacao, "Não foi possível efectuar a ligação ao servidor!", "Mensagem de Erro", JOptionPane.ERROR_MESSAGE);
                        } catch (IOException ex) {
                            //Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                            JOptionPane.showMessageDialog(ligacao, "Não foi possível efectuar a ligação ao servidor!", "Mensagem de Erro", JOptionPane.ERROR_MESSAGE);
                        }
                    } else {
                        JOptionPane.showMessageDialog(ligacao, "O servidor não pode ficar em branco!", "Mensagem de Erro", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }

            @Override
            public void keyReleased(KeyEvent ke) {
            }
        });

        ligacao.setVisible(true);
    }

    /**
     * Mostra o menu onde se pode escolher entre, Fazer Login, Fazer Novo Registo ou Sair
     */
    public void show_menu() {
        final JFrame menu = new JFrame();
        menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        menu.setUndecorated(true);
        menu.setTitle(PS_name + " * Menu");
        Container c_menu = menu.getContentPane();
        c_menu.setLayout(new GridLayout(1, 1));

        ClassLoader cl = this.getClass().getClassLoader();
        String img = "imagens/menu.jpg";
        URL icon = cl.getResource(img);
        ImageIcon Imagem;
        
        if(icon!=null)
            Imagem = new ImageIcon(icon);
        else
            Imagem = new ImageIcon(img);
        
        BGImage fundo = new BGImage(Imagem.getImage());
        center(menu, fundo.getSize());
        fundo.setLayout(new BorderLayout());

        JLabel l_menu = new JLabel("Menu");
        JPanel lm = new JPanel();
        lm.setOpaque(false);
        JButton b_login = new JButton("Login");
        JButton b_registo = new JButton("Novo Registo");
        JButton b_sair = new JButton("Sair");
        JPanel lb = new JPanel();
        lb.setOpaque(false);

        b_sair.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                menu.dispose();
            }
        });

        b_login.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                show_login();
                menu.dispose();
            }
        });

        b_registo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                show_registar();
                menu.dispose();
            }
        });

        lb.setLayout(new FlowLayout());
        lb.add(b_login);
        lb.add(b_registo);
        lb.add(b_sair);
        fundo.add(lb, BorderLayout.CENTER);

        lm.setLayout(new FlowLayout());
        lm.add(l_menu);
        fundo.add(lm, BorderLayout.NORTH);

        c_menu.add(fundo);
        menu.setVisible(true);
    }

    /**
     * Mostra a janela onde irá ser mostrada a informação dos jogos disponíveis para jogar no servidor.
     */
    public void show_rooms() {
        final JFrame rooms = new JFrame();
        try {
            oos.reset();
            oos.writeObject(new Update());
            oos.flush();
            Rooms R = (Rooms) ois.readObject();
            rooms.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            rooms.setUndecorated(true);
            rooms.setTitle(PS_name + " * Jogos");
            Container c_rooms = rooms.getContentPane();
            c_rooms.setLayout(new GridLayout(1, 1));
            ClassLoader cl = this.getClass().getClassLoader();
            String img = "imagens/rooms.jpg";
            URL icon = cl.getResource(img);
            ImageIcon Imagem;

            if(icon!=null)
                Imagem = new ImageIcon(icon);
            else
                Imagem = new ImageIcon(img);

            BGImage fundo = new BGImage(Imagem.getImage());
            center(rooms, fundo.getSize());
            fundo.setLayout(new BorderLayout());

            JLabel l_titulo = new JLabel("Lobby - " + username.getText());
            l_titulo.setForeground(Color.WHITE);
            JPanel titulo = new JPanel();
            titulo.setOpaque(false);
            titulo.setLayout(new FlowLayout());
            titulo.add(l_titulo);
            fundo.add(titulo, BorderLayout.NORTH);

            JButton b_criar = new JButton("Criar Jogo");
            JButton b_juntar = new JButton("Juntar-se");
            JButton b_sair = new JButton("Sair");
            JButton b_update = new JButton("Update");
            JPanel botoes = new JPanel();
            botoes.setOpaque(false);
            botoes.setLayout(new FlowLayout());
            botoes.add(b_criar);
            botoes.add(b_juntar);
            botoes.add(b_update);
            botoes.add(b_sair);
            fundo.add(botoes, BorderLayout.SOUTH);
            
            b_update.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    rooms.dispose();
                    show_rooms();
                }
            });

            b_sair.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    try {
                        oos.reset();
                        oos.writeObject(new Logoff());
                        oos.close();
                        ois.close();
                        socket.close();
                        rooms.dispose();
                    } catch (IOException ex) {
                        Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });

            b_criar.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    show_create_room();
                    rooms.dispose();
                }
            });
            
            b_juntar.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    if(!JogoS.isEmpty()){
                        try {
                            JuntarJogo JJ = new JuntarJogo(JogoS);
                            oos.reset();
                            oos.writeObject(JJ);
                            Confirmacao Cj = (Confirmacao) ois.readObject();
                            
                            if(Cj.getResposta()){
                                show_wait();
                                rooms.dispose();
                            }else{
                                JOptionPane.showMessageDialog(rooms, Cj.getText(), "Erro", JOptionPane.ERROR_MESSAGE);
                                show_rooms();
                                rooms.dispose();
                            }
                            
                        } catch (ClassNotFoundException ex) {
                            Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IOException ex) {
                            Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            });

            JPanel central = new JPanel();
            central.setOpaque(false);
            central.setLayout(new BorderLayout());
            JPanel t = new JPanel();
            t.setOpaque(false);
            t.setLayout(new GridLayout(1, 3));
            JLabel l_n = new JLabel("Nome");
            JLabel l_j = new JLabel("Lugares Disponíveis");
            JLabel l_c = new JLabel("Criador");
            l_n.setForeground(Color.WHITE);
            l_j.setForeground(Color.WHITE);
            l_c.setForeground(Color.WHITE);
            t.add(l_n);
            t.add(l_j);
            t.add(l_c);
            central.add(t, BorderLayout.NORTH);

            JPanel pc = new JPanel();
            pc.setOpaque(false);
            if (R.getRooms().size() > 0) {
                final Color cletra = Color.BLACK;
                final Color cback = Color.WHITE;
                final Color sletra = Color.WHITE;
                final Color sback = Color.RED;
                pc.setLayout(new GridLayout(R.getRooms().size(), 3));
                                
                final LinkedList<JLabel> ll = new LinkedList<JLabel>();
                
                for (int r = 0; r < R.getRooms().size(); r++) {
                    final JLabel l1 = new JLabel(R.getNome(r));
                    l1.setForeground(cletra);
                    l1.setBackground(cback);
                    l1.setOpaque(true);
                    final JLabel l2 = new JLabel(R.getJ(r));
                    l2.setForeground(cletra);
                    l2.setBackground(cback);
                    l2.setOpaque(true);
                    final JLabel l3 = new JLabel(R.getCriador(r));
                    l3.setForeground(cletra);
                    l3.setBackground(cback);
                    l3.setOpaque(true);
                    
                    l1.addMouseListener(new MouseListener() {

                        @Override
                        public void mouseClicked(MouseEvent me) {
                            if(l1.getBackground() == cback){
                                for(int i=0;i<ll.size();i++){
                                    ll.get(i).setForeground(cletra);
                                    ll.get(i).setBackground(cback);
                                }
                                JogoS = l1.getText();
                                l1.setForeground(sletra);
                                l1.setBackground(sback);
                                l2.setForeground(sletra);
                                l2.setBackground(sback);
                                l3.setForeground(sletra);
                                l3.setBackground(sback);
                            }else{
                                JogoS = "";
                                l1.setForeground(cletra);
                                l1.setBackground(cback);
                                l2.setForeground(cletra);
                                l2.setBackground(cback);
                                l3.setForeground(cletra);
                                l3.setBackground(cback);
                            }
                        }

                        @Override
                        public void mousePressed(MouseEvent me) {
                        }

                        @Override
                        public void mouseReleased(MouseEvent me) {
                        }

                        @Override
                        public void mouseEntered(MouseEvent me) {
                        }

                        @Override
                        public void mouseExited(MouseEvent me) {
                        }
                    });
                    
                    l2.addMouseListener(new MouseListener() {

                        @Override
                        public void mouseClicked(MouseEvent me) {
                            if(l2.getBackground() == cback){
                                for(int i=0;i<ll.size();i++){
                                    ll.get(i).setForeground(cletra);
                                    ll.get(i).setBackground(cback);
                                }
                                JogoS = l1.getText();
                                l1.setForeground(sletra);
                                l1.setBackground(sback);
                                l2.setForeground(sletra);
                                l2.setBackground(sback);
                                l3.setForeground(sletra);
                                l3.setBackground(sback);
                            }else{
                                JogoS = "";
                                l1.setForeground(cletra);
                                l1.setBackground(cback);
                                l2.setForeground(cletra);
                                l2.setBackground(cback);
                                l3.setForeground(cletra);
                                l3.setBackground(cback);
                            }
                        }

                        @Override
                        public void mousePressed(MouseEvent me) {
                        }

                        @Override
                        public void mouseReleased(MouseEvent me) {
                        }

                        @Override
                        public void mouseEntered(MouseEvent me) {
                        }

                        @Override
                        public void mouseExited(MouseEvent me) {
                        }
                    });
                    
                    l3.addMouseListener(new MouseListener() {

                        @Override
                        public void mouseClicked(MouseEvent me) {
                            if(l3.getBackground() == cback){
                                for(int i=0;i<ll.size();i++){
                                    ll.get(i).setForeground(cletra);
                                    ll.get(i).setBackground(cback);
                                }
                                JogoS = l1.getText();
                                l1.setForeground(sletra);
                                l1.setBackground(sback);
                                l2.setForeground(sletra);
                                l2.setBackground(sback);
                                l3.setForeground(sletra);
                                l3.setBackground(sback);
                            }else{
                                JogoS = "";
                                l1.setForeground(cletra);
                                l1.setBackground(cback);
                                l2.setForeground(cletra);
                                l2.setBackground(cback);
                                l3.setForeground(cletra);
                                l3.setBackground(cback);
                            }
                        }

                        @Override
                        public void mousePressed(MouseEvent me) {
                        }

                        @Override
                        public void mouseReleased(MouseEvent me) {
                        }

                        @Override
                        public void mouseEntered(MouseEvent me) {
                        }

                        @Override
                        public void mouseExited(MouseEvent me) {
                        }
                    });
                    
                    ll.add(l1);
                    ll.add(l2);
                    ll.add(l3);
                }
                
                for(int i=0; i<ll.size(); i++){
                    JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.setOpaque(false); p.add(ll.get(i));                    
                    pc.add(p);
                }
                    pc.validate();
                    pc.repaint();
            } else {
                pc.setLayout(new FlowLayout());
                JLabel aviso = new JLabel("Não Existem Jogos Criados");
                aviso.setForeground(Color.RED);
                pc.add(aviso);
            }

            central.add(pc, BorderLayout.CENTER);
            fundo.add(central, BorderLayout.CENTER);

            c_rooms.add(fundo);
            rooms.setVisible(true);
        } catch (IOException ex) {
            Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassCastException ex){
            rooms.dispose();
            show_rooms();
        }
    }

    /**
     * Mostra a janela onde poderá ser criado um novo jogo.
     */
    public void show_create_room() {
        final JFrame criar_jogo = new JFrame();
        criar_jogo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        criar_jogo.setUndecorated(true);
        criar_jogo.setTitle(PS_name + " * Criar Novo Jogo");
        Container c_rooms = criar_jogo.getContentPane();
        c_rooms.setLayout(new GridLayout(1, 1));
        ClassLoader cl = this.getClass().getClassLoader();
        String img = "imagens/login.jpg";
        URL icon = cl.getResource(img);
        ImageIcon Imagem;
        
        if(icon!=null)
            Imagem = new ImageIcon(icon);
        else
            Imagem = new ImageIcon(img);
        
        BGImage fundo = new BGImage(Imagem.getImage());
        center(criar_jogo, fundo.getSize());
        fundo.setLayout(new BorderLayout());

        JLabel l_titulo = new JLabel("Criar Novo Jogo");
        JPanel titulo = new JPanel();
        titulo.setOpaque(false);
        titulo.setLayout(new FlowLayout());
        titulo.add(l_titulo);
        fundo.add(titulo, BorderLayout.NORTH);

        JButton b_criar = new JButton("Criar");
        JButton b_voltar = new JButton("Voltar");
        JPanel botoes = new JPanel();
        botoes.setOpaque(false);
        botoes.setLayout(new FlowLayout());
        botoes.add(b_criar);
        botoes.add(b_voltar);
        fundo.add(botoes, BorderLayout.SOUTH);

        JLabel l_nome = new JLabel("Nome do Jogo");
        final JTextField nome = new JTextField(20);
        JLabel l_max = new JLabel("Nº Max Jogadores");
        final JComboBox n_max = new JComboBox();
        final String[] lista = {"2 - Dois", "3 - Três", "4 - Quatro"};
        
        for(int i=0; i<lista.length;i++){
            n_max.addItem(lista[i]);
        }
        
        JLabel l_ia = new JLabel("IA");
        final Checkbox cb2 = new Checkbox("J2");
        final Checkbox cb3 = new Checkbox("J3");
        final Checkbox cb4 = new Checkbox("J4");
        JPanel p_ia = new JPanel(); p_ia.setOpaque(false); p_ia.setLayout(new GridLayout(1,3));
        cb3.setEnabled(false);
        cb4.setEnabled(false);
        p_ia.add(cb2);
        p_ia.add(cb3);
        p_ia.add(cb4);
        
        JPanel centro = new JPanel(); centro.setOpaque(false); centro.setLayout(new GridLayout(3,2));
        centro.add(l_nome);
        centro.add(nome);
        centro.add(l_ia);
        centro.add(p_ia);
        centro.add(l_max);
        centro.add(n_max);
        
        fundo.add(centro, BorderLayout.CENTER);
        
        n_max.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent ie) {
                if(ie.getItem().toString().equals(lista[0])){
                    cb2.setEnabled(true);
                    cb3.setState(false);
                    cb3.setEnabled(false);
                    cb4.setState(false);
                    cb4.setEnabled(false);
                }else if(ie.getItem().toString().equals(lista[1])){
                    cb2.setEnabled(true);
                    cb3.setEnabled(true);
                    cb4.setState(false);
                    cb4.setEnabled(false);
                }else if(ie.getItem().toString().equals(lista[2])){
                    cb2.setEnabled(true);
                    cb3.setEnabled(true);
                    cb4.setEnabled(true);
                }
            }
        });

        b_criar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                boolean valido = true;

                try{
                    if (nome.getText().isEmpty()) {
                        JOptionPane.showMessageDialog(criar_jogo, "É Necessário Um Nome Para O Jogo!", "Erro", JOptionPane.ERROR_MESSAGE);
                        valido = false;
                    } else if (nome.getText().contains(" ")) {
                        JOptionPane.showMessageDialog(criar_jogo, "O Nome Não Pode Conter Espaços!", "Erro", JOptionPane.ERROR_MESSAGE);
                        valido = false;
                    }

                    if(valido){
                        CriarJogo CJ = new CriarJogo(nome.getText(), n_max.getSelectedIndex()+2, cb2.getState(), cb3.getState(), cb4.getState());
                        oos.reset();
                        oos.writeObject(CJ);
                        Comunicacao C;
                        do{
                            C = (Comunicacao) ois.readObject();
                        }while(C.getTipo() != TipoComunicacao.CONFIRMACAO);
                        
                        Confirmacao Cr = (Confirmacao) C;

                        if(Cr.getResposta()){
                            criar_jogo.dispose();
                            show_wait();
                        }else{
                            JOptionPane.showMessageDialog(criar_jogo, Cr.getText(), "Erro", JOptionPane.ERROR_MESSAGE);
                            criar_jogo.dispose(); 
                            show_create_room();                           
                        }
                    }
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                }catch(NumberFormatException e){
                    JOptionPane.showMessageDialog(criar_jogo, "O Número de Jogadores Tem De Ser Entre 2 e 2 xD!", "Erro", JOptionPane.ERROR_MESSAGE);
                    valido = false;
                }

            }
        });

        b_voltar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                show_rooms();
                criar_jogo.dispose();
            }
        });

        c_rooms.add(fundo);
        
        criar_jogo.addWindowListener(new WindowListener() {

            @Override
            public void windowOpened(WindowEvent we) {
                nome.requestFocus();
            }

            @Override
            public void windowClosing(WindowEvent we) {
            }

            @Override
            public void windowClosed(WindowEvent we) {
            }

            @Override
            public void windowIconified(WindowEvent we) {
            }

            @Override
            public void windowDeiconified(WindowEvent we) {
            }

            @Override
            public void windowActivated(WindowEvent we) {
            }

            @Override
            public void windowDeactivated(WindowEvent we) {
            }
        });
        
        criar_jogo.setVisible(true);
    }
    
    /**
     * Mostra a janela onde os jogadores aguardarão até que o jogo esteja cheio e pronto a jogar
     */
    public void show_wait(){
            final JFrame aguarda = new JFrame();
            aguarda.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            aguarda.setUndecorated(true);
            aguarda.setTitle(PS_name + " * Aguarde");
            Container c_aguarda = aguarda.getContentPane();
            c_aguarda.setLayout(new GridLayout(1, 1));
            ClassLoader cl = this.getClass().getClassLoader();
            String img = "imagens/login.jpg";
            URL icon = cl.getResource(img);
            ImageIcon Imagem;

            if(icon!=null)
                Imagem = new ImageIcon(icon);
            else
                Imagem = new ImageIcon(img);

            BGImage fundo = new BGImage(Imagem.getImage());
            center(aguarda, fundo.getSize());
            fundo.setLayout(new BorderLayout());
            
            JPanel p = new JPanel(); p.setOpaque(false); p.setLayout(new FlowLayout());
            JLabel l_aguardar = new JLabel("Aguarde Pelos Outros Clientes");
            l_aguardar.setForeground(Color.WHITE);
            p.add(l_aguardar);
            fundo.add(p, BorderLayout.CENTER);
            
            c_aguarda.add(fundo);
            aguarda.setVisible(true);       
            
            Thread thread = new Thread(){
                        @Override
                            public void run(){
                                try{
                                    Confirmacao C = (Confirmacao) ois.readObject();

                                        if(C.getResposta()){
                                            show_game();
                                            aguarda.dispose();
                                        }

                                } catch (IOException ex) {
                                    Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (ClassNotFoundException ex) {
                                    Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }
                        };
            thread.start();        
    }
    
    private JScrollBar vbar;
    private BGImage fundo_game;

    /**
     * Mostra a janela onde irá decorrer o jogo
     */
    private void show_game() {
        final JFrame jogo = new JFrame();
        jogo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jogo.setUndecorated(true);
        jogo.setTitle(PS_name + " * Jogo");
        Container c_jogo = jogo.getContentPane();
        c_jogo.setLayout(new BorderLayout());
        // Imagem 800 * 565
        ClassLoader cl = this.getClass().getClassLoader();
        String img = "imagens/fundo.png";
        URL icon = cl.getResource(img);
        ImageIcon Imagem;
        
        if(icon!=null)
            Imagem = new ImageIcon(icon);
        else
            Imagem = new ImageIcon(img);
        
        fundo_game = new BGImage(Imagem.getImage());
        //fundo2 = new BGImage(Imagem.getImage());
        Dimension s = new Dimension(800,700);
        center(jogo, s);
        fundo_game.setLayout(new GridLayout(17,25));
        
        JPanel painel_inferior = new JPanel(); painel_inferior.setLayout(new BorderLayout());
        JPanel chat = new JPanel(); chat.setLayout(new BorderLayout()); chat.setOpaque(false);
        final JTextArea chat_area = new JTextArea();
        //chat_area.setPreferredSize(new Dimension(100,115));
        chat_area.setEditable(false);
        chat_area.setBackground(Color.WHITE);
        chat_area.setLineWrap(true);
        chat_area.setWrapStyleWord(true);
        
        JScrollPane sp = new JScrollPane(chat_area);        
        vbar = sp.getVerticalScrollBar();
                
        chat.add(sp, BorderLayout.CENTER);
        chat.setPreferredSize(new Dimension(310,150));
        
        JPanel chat_s = new JPanel(); chat_s.setLayout(new FlowLayout()); chat.setOpaque(false);
        final JTextField chat_texto = new JTextField(18);
        
        chat_texto.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent ke) {
            }

            @Override
            public void keyPressed(KeyEvent ke) {
                if(ke.getKeyCode() == KeyEvent.VK_ENTER){
                    try {
                        Chat C = new Chat(username.getText(), chat_texto.getText());
                        oos.reset();
                        oos.writeObject(C);
                        oos.flush();
                        chat_texto.setText("");
                    } catch (IOException ex) {
                        Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }

            @Override
            public void keyReleased(KeyEvent ke) {
            }
        });
        
        JButton chat_enviar = new JButton("Enviar");
        chat_enviar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    Chat C = new Chat(username.getText(), chat_texto.getText());
                    oos.reset();
                    oos.writeObject(C);
                    oos.flush();
                    chat_texto.setText("");
                } catch (IOException ex) {
                    Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        chat_s.add(chat_texto, BorderLayout.CENTER);
        chat_s.add(chat_enviar, BorderLayout.EAST);
        chat.add(chat_s, BorderLayout.SOUTH);
        
        info = new JPanel(); info.setLayout(new BorderLayout()); info.setOpaque(false);
        
        
        painel_inferior.add(chat, BorderLayout.EAST);
        painel_inferior.add(info, BorderLayout.WEST);
        
        c_jogo.add(fundo_game, BorderLayout.CENTER);
        c_jogo.add(painel_inferior, BorderLayout.SOUTH);
        
        jogo.setVisible(true);
        
        fundo_game.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent me) {
                try {
                    Clique c = new Clique(me);
                    oos.reset();
                    oos.writeObject(c);
                    oos.flush();
                } catch (IOException ex) {
                    Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            @Override
            public void mousePressed(MouseEvent me) {
            }

            @Override
            public void mouseReleased(MouseEvent me) {
            }

            @Override
            public void mouseEntered(MouseEvent me) {
            }

            @Override
            public void mouseExited(MouseEvent me) {
            }
        });
        
        Thread escuta = new Thread(){
          public void run(){
                try {
                    boolean ligado = true;
                    Comunicacao C = null;
                    while(!interrupted() && ligado){
                        Object O;
                        
                        do{
                            O = ois.readObject();
                            try{
                                C = (Comunicacao) O;
                            }catch (ClassCastException e){

                            }
                        }while(O instanceof String);

                        if(C.getTipo() == TipoComunicacao.MAPA){
                            CMapa cm = (CMapa) C;
                            m = cm.getMapa();
                            actualizamapa(m);
                        }else if(C.getTipo() == TipoComunicacao.CHAT){
                            Chat c = (Chat) C;
                            chat_area.append(c.getNick()+": "+c.getMensagem()+"\n");
                            
                            if( ((vbar.getValue() + vbar.getVisibleAmount()) == vbar.getMaximum()) ) 
                                chat_area.setCaretPosition( chat_area.getDocument().getLength() );
                        }else if(C.getTipo() == TipoComunicacao.INFO){
                            Info I = (Info) C;
                            // painel chat que é um borderlayout
                            info.removeAll();
                            if(I.getTO() == TipoObjecto.VAZIO){
                                
                            }else if(I.getTO() == TipoObjecto.TREINADOR){
                                info.add(new JLabel(I.getNome()), BorderLayout.NORTH);
                                JTextArea t = new JTextArea(); t.setEditable(false);
                                t.setText("Vida: "+I.getVida()+"\nAtaque: "+I.getAtaque()+"\nRange: "+I.getRange()+"\nVelocidade: "+I.getVelocidade());
                                info.add(t, BorderLayout.CENTER);
                            }else if(I.getTO() == TipoObjecto.POKEMON){
                                info.add(new JLabel(I.getNome()), BorderLayout.NORTH);
                                JTextArea t = new JTextArea(); t.setEditable(false);
                                t.setText("Vida: "+I.getVida()+"\nAtaque: "+I.getAtaque()+"\nRange: "+I.getRange()+"\nVelocidade: "+I.getVelocidade());
                                info.add(t, BorderLayout.CENTER);
                            }else if(I.getTO() == TipoObjecto.EDIFICIO){
                                
                            }else if(I.getTO() == TipoObjecto.OBSTACULO){
                                info.add(new JLabel(I.getNome()), BorderLayout.NORTH);
                            }
                            info.validate();
                            jogo.validate();
                            info.repaint();
                            jogo.repaint();
                        }else if(C.getTipo() == TipoComunicacao.LOGOUT){
                            ligado = false;
                            JOptionPane.showMessageDialog(jogo, ((Logout)C).getText(), "Game Over!", JOptionPane.INFORMATION_MESSAGE);
                            Chat off = new Chat(username.getText(), "Perdi o jogo!");
                            oos.reset();
                            oos.writeObject(off);
                            oos.flush();
                            jogo.dispose();
                        }
                    }
                } catch (IOException ex) {
                    //Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(jogo, "Ocorreram problemas no Servidor", "Erro no Servidor", JOptionPane.ERROR_MESSAGE);
                    jogo.dispose();
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(PokeStar.class.getName()).log(Level.SEVERE, null, ex);
                }
          }

            private void actualizamapa(Mapa m) {
                Objecto[][] matriz = m.getMatriz();
                fundo_game.removeAll();
                for(int x=0; x<matriz.length; x++){
                    for(int y=0; y<matriz[x].length; y++){
                        ClassLoader cl = this.getClass().getClassLoader();
                        String img = "imagens/"+matriz[x][y].getNome()+".png";
                        URL icon = cl.getResource(img);
                        ImageIcon Imagem;

                        if(icon!=null)
                            Imagem = new ImageIcon(icon);
                        else
                            Imagem = new ImageIcon(img);

                        fundo_game.add(new JLabel(Imagem));
                    }
                }
                fundo_game.validate();
                jogo.validate();
                fundo_game.repaint();
                jogo.repaint();
            }
        };
        escuta.start();
    }
}